/* 4 buyers */
/* 4 buyers, construction only*/
*global threshold_rd = .2
set seed 281920
global threshold_fg = .01
do graph_table_sub.do
do load_dataset.do
do procurement_type_classification.do
global graph_filename "work_only"

foreach outcomevar in "lnblog" /*"rblog"*/{
	foreach buyer in "Georgia" /*"Caltrans"*/ "Malta" "Mongolia" {
		di "*************** `buyer' *****************"
		local TABLE_combined = ""
		if "`buyer'" == "Georgia"{
			use ../data/georgia/sample_georgia26may22.dta, clear /* NAT==1only */
			qui gen delta_low1 = pdelta_first
			qui gen delta_low2 = pdelta23_first
			qui gen delta_win1 = pdeltaw_first
			qui gen delta_win2 = pdeltaw23_first
		
			qui gen delta_Nlow1 = ldelta_first 
			qui gen delta_Nlow2 = ldelta23_first 
			
			*drawgraph_1x2_days delta_Nlow1 lnblog "" "`buyer'" "`buyer'" "graph_stats_notable" "30 60"
			*drawgraph_1x2_days delta_Nlow1 lnblog "" "`buyer'" "`buyer'" "graph_stats_notable" "90 120"
			
		}
		else if "`buyer'" == "Caltrans"{
			use ../data/caltrans/sample_caltrans29Dec24.dta, clear
			rename delta delta_low1
			rename delta23 delta_low2
			*drop Bwin Bwinbid
			*keep contract_id delta* lnblog*
			qui gen delta_Nlow1 = ldelta 
			qui gen delta_Nlow2 = ldelta23
			
			*drawgraph_1x2_days delta_Nlow1 lnblog "" "`buyer'" "`buyer'" "graph_stats_notable" "30 60"
			*drawgraph_1x2_days delta_Nlow1 lnblog "" "`buyer'" "`buyer'" "graph_stats_notable" "90 120"
			
			
		}
		else if "`buyer'" == "Malta"{
			*use "..\Data\Malta\malta.dta", clear
			use ../data/Malta/malta.dta, clear
			keep if Evaluation_Method == "Price/Cost Effectiveness" & eAuctions=="No"
		    rename deltap delta_win1
			rename delta2p delta_win2
		    rename delta delta_low1
			rename delta2 delta_low2
			
		}
		else if "`buyer'" == "Mongolia"{				
			use ../data/mongolia/sample_mongolia_30jun22.dta, clear
			rename delta1w delta_win1
			rename delta2w delta_win2
			rename delta1 delta_low1
			rename delta2 delta_low2
		}
		standardize_Procurement_Type "`buyer'"
		/**/qui ds
		if ustrregexm("`r(varlist)'", "Procurement_Type") & "`buyer'" != "Caltrans"{
			keep if ustrregexm(Procurement_Type, "[wW]orks?")==1
		}
		/**/
		* loop for main and placebo
		forvalues z = 1(1)1{
			drawgraph_sub `outcomevar' delta_low`z' "`condition'" "30 60 90 120" "0" "`GRAPH'" "`TABLE'" "`buyer'"
			foreach e in `r(TABLE)' {
				local TABLE_combined = "`TABLE_combined' `e'"
			}
			/**/
			*cap noisily drawgraph_1x2_days delta_low`z' lnblog "" "`buyer'" "`buyer'" "graph_stats_notable" "30 60"
			*cap noisily drawgraph_1x2_days delta_low`z' lnblog "" "`buyer'" "`buyer'" "graph_stats_notable" "90 120"
			*drawgraph_1x2_days delta_win2 lnblog "" "`buyer'" "Placebo, `buyer'" "graph_stats_notable" "30 60"
			*drawgraph_1x2_days delta_win2 lnblog "" "`buyer'" "Placebo, `buyer'" "graph_stats_notable" "90 120"
			*do georgia/bycategory.do
			
			/**/

			if ("`buyer'" == "Georgia"){
				esttab `TABLE_combined' using ../Output/rdtable_4buyers_combined_delta_low`z'_work_only.tex, ///
				f replace cells(none) collabels(none) title("`runningvar'") nonumbers ///
				stats(tau_bc se_tau_rb pv_rb h_r /*b_l *kernel bwselect*/ N, fmt(/*%6.0g*/3 3 3 3 "%9.0fc" ) ///
				labels(`"\raisebox{-2.3mm}[0mm][0mm]{$\hat{\beta}$}"' `" "' `"\it{p-value}"' `"\it{h}"' `"Obs."') ///
				layout(@ (@) [@] @ @) star(tau_bc)) starlevels(* 0.10 ** 0.05 *** 0.01) ///
				mtitles("30 Days" "60 Days" "90 Days" "120 Days" ) ///
				/*posthead(\hline &\multicolumn{8}{c}{ Log Backlog } \\ )*/ ///
				posthead(\\[-2mm] &\multicolumn{4}{c}{`buyer'} \\ ///
					 \cmidrule(l){2-5})			
			}
			*else if ("`buyer'" == "Caltrans"){
			*	di "skip
			*}
			
			else {
				esttab `TABLE_combined'  using ../Output/rdtable_4buyers_combined_delta_low`z'_work_only.tex, ///
				f append cells(none) collabels(none) title("`runningvar'") nonumbers ///
				stats(tau_bc se_tau_rb pv_rb h_r /*b_l *kernel bwselect*/ N, fmt(/*%6.0g*/3 3 3 3 "%9.0fc" ) ///
				labels(`"\raisebox{-2.3mm}[0mm][0mm]{$\hat{\beta}$}"' `" "' `"\it{p-value}"' `"\it{h}"' `"Obs."') ///
				layout(@ (@) [@] @ @) ) nomtitles ///
				/*posthead(\hline &\multicolumn{4}{c}{Malta}&\multicolumn{4}{c}{Mongolia}\\ )*/ ///
				posthead(\\[-2mm] &\multicolumn{4}{c}{`buyer'} \\ ///
								 \cmidrule(l){2-5})
			}
		}
	}
}




